<template>
  <!-- 注册页面 -->
  <div style="width: 500px; margin: 0 auto;">
    <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="100px" class="register-form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="registerForm.username"  auto-complete="off"></el-input>
      </el-form-item>
      <!-- 展示昵称框 -->
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="registerForm.nickName"  auto-complete="off"></el-input>
      </el-form-item>
      <!-- 展示密码框 -->
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="registerForm.password" auto-complete="off"></el-input>
      </el-form-item>
      <!-- 展示确认密码框 -->
      <el-form-item label="确认密码" prop="confirm_password">
        <el-input type="password" v-model="registerForm.confirm_password" auto-complete="off"></el-input>
      </el-form-item>
      <!-- 邮箱 -->
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="registerForm.email" auto-complete="off"></el-input>
      </el-form-item>
      <!-- 邀请码 -->
      <el-form-item label="邀请码" prop="inviteCode">
        <el-input v-model="registerForm.inviteCode" auto-complete="off"></el-input>
      </el-form-item>
      <!--展示性别:1是男性，2是女性-->
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="registerForm.gender">
          <el-radio label="1">男性</el-radio>
          <el-radio label="2">女性</el-radio>
          <el-radio label="3">保密</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 展示手机号 -->
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="registerForm.phone"></el-input>
      </el-form-item>
      <!-- 身份按钮，是学生还是教师 -->
      <el-form-item label="身份选择" prop="role">
        <el-radio-group v-model="registerForm.role">
          <el-radio :label="1">学生</el-radio>
          <el-radio :label="2">教师</el-radio>
          <el-radio :label="3">管理员</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('registerForm')">注册</el-button>
        <el-button @click="resetForm('registerForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import {register}  from '@/api/register'

export default {
  name: 'Register',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.confirm_password !== '') {
          this.$refs.registerForm.validateField('confirm_password');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      registerForm: {
        username: '',
        email: '',
        gender:"1",
        phone: '',
        password: '',
        confirm_password: '',
        role:1,
        nickName:"",
        inviteCode:""
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        confirm_password: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 11, max:11,message: '密码长度为11位', trigger: 'blur' },
        ],
        userType: [
          { required: true, message: '请选择您的身份', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register(this.registerForm).then(ret => {
            this.$message({
              message: '注册成功',
              success: true,
              type: 'success'
            });
            // 注册成功之后重置表单
            this.resetForm('registerForm');
            this.$router.push({ path: '/login' });
          }).catch(error => {
            this.$message.error('注册失败，请检查输入信息');
          });
        } else {
          console.log('错误提交!!');
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.register-form {
  margin-top: 20px;
}
</style>
